<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="时间" style="width: 308px;">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mobile:stat:export']">导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExportWeek" v-hasPermi="['mobile:weekstat:exportLastWeek']">最近7天导出
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExportMonth" v-hasPermi="['mobile:monthstat:exportLastMonth']">最近30天导出
        </el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" height="800" :data="statList" @selection-change="handleSelectionChange">
      <el-table-column label="主键ID" align="center" prop="id" v-if="false" />
      <el-table-column fixed label="用户名" align="center" prop="nickname" />
      <el-table-column label="手机号" align="center" prop="phone" />
<!--      <el-table-column label="绑定时间" align="center" prop="bindTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.bindTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="统计时间" align="center" prop="statDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.statDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行走距离(m)" align="center" prop="distance" />
      <el-table-column label="总步数" align="center" prop="steps" />
      <el-table-column label="消耗卡路里(千卡)" align="center" prop="calories" />
      <el-table-column label="最大心率(次/分钟)" align="center" prop="maxHeartrate" />
      <el-table-column label="最小心率(次/分钟)" align="center" prop="minHeartrate" />
      <el-table-column label="平均心率(次/分钟)" align="center" prop="meanHeartrate" />
      <el-table-column label="最大血氧饱和度(%)" align="center" prop="maxSpo2" />
      <el-table-column label="最小血氧饱和度(%)" align="center" prop="minSpo2" />
      <el-table-column label="最平均血氧饱和度(%)" align="center" prop="meanSpo2" />

    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改华为健康统计数据对话框 -->

  </div>
</template>

<script setup name="Stat">
import { listStat, getStat, delStat, addStat, updateStat } from "@/api/mobile/stat";

const { proxy } = getCurrentInstance();

const statList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nickname: null,
    phone: null
  },
  rules: {
    userId: [
      { required: true, message: "user.id不能为空", trigger: "blur" }
    ],
    steps: [
      { required: true, message: "总步数不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询华为健康统计数据列表 */
function getList() {
  loading.value = true;
  listStat(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    statList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    statDate: null,
    distance: null,
    steps: null,
    calories: null,
    maxHeartrate: null,
    minHeartrate: null,
    meanHeartrate: null,
    maxSpo2: null,
    minSpo2: null,
    meanSpo2: null
  };
  proxy.resetForm("statRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加华为健康统计数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStat(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改华为健康统计数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["statRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStat(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStat(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除数据项？').then(function () {
    return delStat(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mobile/stat/export', {
    ...queryParams.value
  }, `stat_${new Date().getTime()}.xlsx`)
}


function handleExportWeek() {
  proxy.download('mobile/weekstat/exportLastWeek', {
    ...queryParams.value
  }, `stat_${new Date().getTime()}.xlsx`)
}

function handleExportMonth() {
  proxy.download('mobile/monthstat/exportLastMonth', {
    ...queryParams.value
  }, `stat_${new Date().getTime()}.xlsx`)
}

getList();
</script>
